package org.xjc;

import lombok.extern.slf4j.Slf4j;

import org.flowable.engine.*;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;

@Slf4j
public class Main {
    public static void main(String[] args) {
        //创建工作流引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取流程存储服务
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //部署流程运行时服务
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //获取流程任务
        TaskService taskService = processEngine.getTaskService();
        //部署流程
        repositoryService.createDeployment()
                .addClasspathResource("process/leaveApplyProcess.bpmn20.xml")
                .deploy();
        log.info("部署成功");

        runtimeService.startProcessInstanceByKey("leaveApplyProcess");
        log.info("启动流程实例成功");
        TaskQuery taskQuery = taskService.createTaskQuery();
        //查询第一个任务
        Task firstTask = taskQuery.taskAssignee("liuxiaopeng").singleResult();
        //完成第一个任务
        taskService.complete(firstTask.getId());
        log.info("用户任务{}办理完成，办理人为：{}", firstTask.getName(), firstTask.getAssignee());
        //查询第二个任务
        Task secondTask = taskQuery.taskAssignee("hebo").singleResult();
        //完成第二个任务
        taskService.complete(secondTask.getId());
        log.info("用户任务{}办理完成，办理人为：{}", secondTask.getName(), secondTask.getAssignee());
        //查询任务数
        long count = taskQuery.count();
        log.info("任务数：{}", count);

        //关闭工作流
        processEngine.close();
    }
}